package edu.towson.cosc.cosc455.mdapice.compiler;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.*;

/**
 * COSC 455 Programming Languages: Implementation and Design.
 * 
 * @author Michelle D'Apice
 */

public class MyCompiler {
	
	public static String token; //to house current token
	public static String fileContent; //to house String from input file
	public static ArrayList<String> parseTree = new ArrayList<String>();
	
	//creating objects to have access to these classes
	static MyLexicalAnalyzer Lexer;
	static MySyntaxAnalyzer parser;
	static MySemanticAnalyzer generator;

	public static void main(String[] args) throws IOException {
		
		//make sure token is empty
		token = "";
		
		//can't have more than one file, can't have less than one file
		//args = command line argument
		//Check that there is 1 argument
		if(args.length != 1){
			System.err.println("ERROR: Only one file can be compiled at a time.");
			System.exit(0);
		}
		
		//check extension -- must be .lol
		else{
			String filename = args[0];
			int dot = filename.indexOf('.');
			String ext = filename.substring(dot);
			if(!ext.equals(".lol")){
				System.err.println("ERROR: File must have extention .lol.");
				System.exit(0);
			}
		}
		
		//read entire file into String
		FileReader fr = new FileReader(args[0]);
		BufferedReader br = new BufferedReader(fr);
		StringBuffer fileContents = new StringBuffer();
		String contentLine = br.readLine();
		while(contentLine != null){
			fileContents.append(contentLine);
			contentLine = br.readLine();
		}
		
		//read into string to be processed by Lexical Analyzer
		fileContent = fileContents.toString();
		//taking care of tabs and newlines
		fileContent = fileContent.replace("\n", " ").replace("\r", " ").replace("\t", " ").replace("  ", " ");
		
		
		/*Two steps:
		 * get first token using LexicalAnalyzer's getToken()
		 * call syntax analyzer's start state (implementing BNF grammar)
		 */
		
		//chop up file into tokens
		//gets token, puts it into bin (in MyLexicalAnalyzer) and quits
		Lexer = new MyLexicalAnalyzer();
		Lexer.getNextToken();
		
		//ready to start parsing/syntactically checking
		parser = new MySyntaxAnalyzer();
		parser.lolcode(); //start state - in MySyntaxAnalyzer
		//^this will run through the entire thing
		
		//will come back with parse tree if parsed correctly
		//need to give it meaning after parsing 
		generator = new MySemanticAnalyzer();
		//generator.doSomethingElse();
		generator.semanticAn();
		//call Semantic Analyzer
		
		//generating output file name with .html extension
		String filename = args[0];
		int dot = filename.indexOf('.');
		String outputFile = filename.substring(0, dot) + ".html";
		
		//writing output file
		File file = new File(outputFile);
        BufferedWriter output = new BufferedWriter(new FileWriter(file));
        for(String value : MySemanticAnalyzer.finalTree){
        	output.write(value);
        }
        
        //cleanup - close files
        output.close();
		br.close();
	}

}